#include <highgui.h>
#include <iostream>
#include <cv.h>

#include "resourceTracker.h"
using namespace cv;
using namespace std;

/*
 *  AVITagger.cpp
 *  
 *
 *  Created by Rasmus Kyng on 11/01/2011.
 *  Copyright 2011 __MyCompanyName__. All rights reserved.
 
 
 Compatibility: Tested on Mac, should run on Windows, may not run on Linux.
 
 Attribution: This uses code bits from the book Learning OpenCV.
 
 LOCV notes:
 
 License:
 Oct. 3, 2008
 Right to use this code in any way you want without warrenty, support or any guarentee of it working.
 
 Learning OpenCV: Computer Vision with the OpenCV Library
 by Gary Bradski and Adrian Kaehler
 Published by O'Reilly Media, October 3, 2008
 *
 */

//Variables

//Horrible dependencies on global variables here! How I wish I could use Java and have nice GC.
Mat frame;
cv::Ptr< cv::VideoCapture > capture;

ResourceTracker* rt;


//Window name for display
char* windowName = "AVI Frame Tagger";


//Defining the callback function to install for mouse events

void mouse_callback_TagAndNextFrame( int event, int x, int y, int flags, void* NOTUSED) {
	
	switch (event) {
		case CV_EVENT_LBUTTONUP:
			if (!frame.empty()) {
				std::cout << "x = " << x << "; y = " << y << std::endl; //print click location

				rt->tagVideo( x, y);
				
				*capture >> frame; //get next frame
				
				if (frame.empty()) {
					std::cout << "No frames left. Press ESC to exit" << std::endl;
				} else {
					imshow( windowName, frame ); //show next frame
				}
			}
			break;
		default:
			break;
	}
}


int main( int argc, char** argv ) {
	
	if (argc != 2) {
		cerr << "Usage: AVITagger videoName" << endl;
		return -1;
	}
	
	while(1) {
		std::cerr << "Press y+ENTER to start tagging, or n+ENTER to cancel." << std::endl;
		char c;
		cin >> c;
		if( c == 'n' ) return 0; //exit if user presses escape
		if( c == 'y' ) break;
	}
	
	char* inputVideoNameString = argv[1];
	
	namedWindow( windowName, CV_WINDOW_AUTOSIZE );
	
	rt = new ResourceTracker( "../config/runtimeSettings/cfg.xml" );
	rt->loadCategory("TestData");
	
	rt->startVideoTagging( inputVideoNameString );
	
	capture = rt->getFileVideoCapture( inputVideoNameString );
	
	*capture >> frame;
	
	if( frame.empty() ) {
		std::cerr << "No frames found" << std::endl;
		delete rt;
		return -1;
	}
	
	imshow( windowName, frame ); //show the first frame so user may begin tagging
	
	cvSetMouseCallback( windowName, mouse_callback_TagAndNextFrame, NULL);
	//set mouse callback to TagAndNextFrame.
	//0 means no special data is passed
	
	while(1) {
		char c = waitKey(0);
		if( c == 27 ) break; //exit if user presses escape
	}
	
	rt->stopVideoTagging();

// TODO Support cancel of tagging
//	std::cout << "Would you like to store the sequence of point tags? (y or n)" << std::endl;
//	
//	waitKey(100); //Give the terminal a moment to write the above...
//	
//	char ans;
//	
//	std::cin >> ans;
//	
//	std::cout << ans << std::endl;
//	
//	bool requestInput = true;
//	
//	while( requestInput ) {
//		switch (ans) {
//			case 121: //ASCII y = 121
//				storeData( inputVideoNameString, tagCount, handLocations ); 
//				requestInput = false;
//				break;
//			case 110: //ASCII y = 121
//				requestInput = false;
//				break;	
//			default:
//				std::cout << "Input not recognised. Try again." << std::endl;
//				std::cin >> ans;
//				break;
//		}
//	}
	
	// -- Clean up -- //
	
	delete rt;
	
	return 0;
}